package com.wanxi.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanxi.dao.NewsDao;
import com.wanxi.dao.NewsTypeDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.News;
import com.wanxi.model.NewsType;
import com.wanxi.service.NewsTypeService;
import com.wanxi.util.CreateOnlyID;
import com.wanxi.util.JsonCopy;
import com.wanxi.vo.NewsTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class NewsTypeServiceImpl implements NewsTypeService {
    @Autowired
    NewsTypeDao newsTypeDao;
    @Autowired
    CreateOnlyID createOnlyId;
    @Autowired
    NewsDao newsDao;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public ResultDTO findAllNewsType() {
        QueryWrapper<NewsType> queryWrapper=new QueryWrapper<>();
        List<NewsType> list =newsTypeDao.selectList(queryWrapper);
        if(list.size()>0){
            return ResultDTO.getDTO(list.size(),list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }

    @Override
    public ResultDTO findNewsTypeById(NewsTypeVO newsTypeVO) {
        NewsType newsType= JsonCopy.copy(newsTypeVO, NewsType.class);
        NewsType nt=newsTypeDao.selectById(newsType.getId());
        if(nt!=null){
            return ResultDTO.getDTO(1,nt);
        }
        return ResultDTO.builder().count(0).msg("error").code(1).build();
    }

    @Override
    public ResultDTO updateEnable(NewsTypeVO newsTypeVO) {
        NewsType newsType= JsonCopy.copy(newsTypeVO, NewsType.class);
        newsType.setUpdateTime(new Date());
        int i=newsTypeDao.updateById(newsType);
        if(i==1){
            stringRedisTemplate.delete("newsType");
            return ResultDTO.getDTO(i,newsTypeVO);
        }
        return ResultDTO.builder().data(newsTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO addNewsType(NewsTypeVO newsTypeVO) {
        NewsType newsType= JsonCopy.copy(newsTypeVO, NewsType.class);
        newsType.setId(createOnlyId.createId());
        newsType.setUpdateTime(new Date());
        newsType.setCreateTime(new Date());
        int i=newsTypeDao.insert(newsType);
        if(i==1){
            stringRedisTemplate.delete("newsType");
            return ResultDTO.getDTO(i,newsTypeVO);
        }
        return ResultDTO.builder().data(newsTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO deleteNewsType(NewsTypeVO newsTypeVO) {
        NewsType newsType= JsonCopy.copy(newsTypeVO, NewsType.class);
        News news = News.builder()
                .newsType(NewsType.builder()
                        .id(newsType.getId())
                        .build())
                .build();
        if(newsDao.findNews(news).size()<1){
            int i=newsTypeDao.deleteById(newsType);
            if(i==1){
                stringRedisTemplate.delete("newsType");
                return ResultDTO.getDTO(i,newsTypeVO);
            }
        }
        return ResultDTO.builder().data(newsTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO updateNewsType(NewsTypeVO newsTypeVO) {
        NewsType newsType= JsonCopy.copy(newsTypeVO, NewsType.class);
        newsType.setUpdateTime(new Date());
        int i=newsTypeDao.updateById(newsType);
        if(i==1){
            stringRedisTemplate.delete("newsType");
            return ResultDTO.getDTO(i,newsTypeVO);
        }
        return ResultDTO.builder().data(newsTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO findNewsType() {
        Long len = stringRedisTemplate.opsForList().size("newsType");
//        判断redis中是否有数据
        if(len!=0){
//            返回redis中的数据,通过fastJson转换
            List<String> redisNewsType = stringRedisTemplate.opsForList().range("newsType",0,len);
            List<NewsType> list= JSON.parseObject(redisNewsType.toString(), new TypeReference<List<NewsType>>() {});
            return ResultDTO.getDTO(list.size(),list);
        }
//        没有则从数据库中找数据
        List<NewsType> list =newsTypeDao.selectList(null);
        if(list.size()>0){
//            Dao层数据迭代
            Iterator<NewsType> iterator=list.iterator();
            while (iterator.hasNext()){
                NewsType nt= iterator.next();
                if (nt.getEnable()==0){
                    iterator.remove();
                }else{
//                    将查到的数据放入redis
                    stringRedisTemplate.expire("newsType",300, TimeUnit.SECONDS);
                    stringRedisTemplate.opsForList().rightPush("newsType", JSON.toJSONString(nt));
                }

            }
            return ResultDTO.getDTO(list.size(),list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }
}
